import sys
import argparse
from itertools import groupby

def sort_tsv_by_key(input_file, output_file):
    # 读取文件并按前三列分组
    with open(input_file, 'r') as f:
        # 按照前三列作为键进行排序
        lines = sorted(f, key=lambda line: tuple(line.split('\t')[:3]))
    
    # 写入到输出文件
    with open(output_file, 'w') as f_out:
        for line in lines:
            f_out.write(line)

def main():
    parser = argparse.ArgumentParser(description="Sort a TSV file by the first three columns as keys.")
    parser.add_argument("input_file", help="Input TSV file path")
    parser.add_argument("output_file", help="Output sorted TSV file path")

    args = parser.parse_args()

    # 执行排序
    sort_tsv_by_key(args.input_file, args.output_file)

if __name__ == "__main__":
    main()
